home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1997 July / macformat52.iso / mac / Shareware Plus / Developers / YAAF v1.0 alpha 1 / (Samples) / Text Display / Code / TTextView.cpp < prev   
Encoding:
C/C++ Source or Header  |  1997-04-21  |  2.9 KB  |  161 lines

  1. /*    TTextView.cpp
  2.  *
  3.  *        This contains my code to handle text views
  4.  */
  5.  
  6. #include "MyApp.h"
  7. #include <XStdScrollView.h>
  8.  
  9. /************************************************************************/
  10. /*                                                                        */
  11. /*    Construction/Destruction                                            */
  12. /*                                                                        */
  13. /************************************************************************/
  14.  
  15. /*    TTextView::TTextView
  16.  *
  17.  *        Construct this thing
  18.  */
  19.  
  20. TTextView::TTextView(XGView *parent, XGArgStream &s) : XGView(parent,s)
  21. {
  22.     fFile = NULL;
  23. }
  24.  
  25. /*    TTextView::~TTextView
  26.  *
  27.  *        Destruct this thing
  28.  */
  29.  
  30. TTextView::~TTextView()
  31. {
  32.     if (fFile) fclose(fFile);
  33. }
  34.  
  35. /************************************************************************/
  36. /*                                                                        */
  37. /*    Control                                                                */
  38. /*                                                                        */
  39. /************************************************************************/
  40.  
  41. /*    TTextView::ShowFile
  42.  *
  43.  *        Show this thing
  44.  */
  45.  
  46. void TTextView::ShowFile(FILE *f)
  47. {
  48.     char buffer[256];
  49.     
  50.     fFile = f;
  51.     if (fFile == NULL) return;
  52.     
  53.     fseek(f,0,SEEK_SET);
  54.     fStart += ftell(f);
  55.     while (fgets(buffer,255,f)) fStart += ftell(f);
  56.     
  57.     RecalcScroll();
  58.     InvalView();
  59. }
  60.  
  61. /*    TTextView::DoSizeView
  62.  *
  63.  *        Resize
  64.  */
  65.  
  66. void TTextView::DoSizeView()
  67. {
  68.     XGView::DoSizeView();
  69.     RecalcScroll();
  70. }
  71.  
  72. /*    TTextView::DoDrawView
  73.  *
  74.  *        Draw me
  75.  */
  76.  
  77. void TTextView::DoDrawView(Rect r)
  78. {
  79.     XGScrollView *v;
  80.     char buffer[256];
  81.     long s,e,l;
  82.     short x,y;
  83.     char *ptr;
  84.     Rect t;
  85.     
  86.     // initialize
  87.     v = dynamic_cast<XGScrollView *>(GetParent());
  88.     if (v == NULL) return;
  89.     XGDraw draw(this);
  90.     
  91.     // start scrolling range
  92.     v->GetScrollValue(&x,&y);
  93.     s = y + r.top / 12;
  94.     e = y + (r.bottom + 11) / 12;
  95.     if (e >= fStart.Length()) e = fStart.Length() - 1;
  96.     
  97.     // draw the lines of text
  98.     t = r;
  99.     for (l = s; l < e; l++) {
  100.         fseek(fFile,fStart[l],SEEK_SET);
  101.         fgets(buffer,255,fFile);
  102.         for (ptr = buffer; (*ptr != 0) && (*ptr != '\n'); ptr++) ;
  103.         *ptr = 0;
  104.         
  105.         t.top = (l-y)*12;
  106.         t.bottom = t.top + 12;
  107.         
  108. #if OPT_MACOS == 1
  109.         ::EraseRect(&t);
  110.         ::MoveTo(5,(l-y)*12+10);
  111.         ::DrawString(c2pstr(buffer));
  112. #endif
  113.  
  114. #if OPT_WINOS == 1
  115.         ::FillRect(draw.GetDC(),&t,GetStockObject(WHITE_BRUSH));
  116.         ::TextOut(draw.GetDC(),5,(l-y)*12,buffer,strlen(buffer));
  117. #endif
  118.     }
  119.     
  120.     if (t.bottom < r.bottom) {
  121.         r.top = t.bottom;
  122. #if OPT_MACOS == 1
  123.         ::EraseRect(&r);
  124. #endif
  125.  
  126. #if OPT_WINOS == 1
  127.         ::FillRect(draw.GetDC(),&r,GetStockObject(WHITE_BRUSH));
  128. #endif
  129.     }
  130. }
  131.  
  132. /************************************************************************/
  133. /*                                                                        */
  134. /*    Scroll management                                                    */
  135. /*                                                                        */
  136. /************************************************************************/
  137.  
  138. /*    TTextView::RecalcScroll
  139.  *
  140.  *        Recalculate the scrollbar
  141.  */
  142.  
  143. void TTextView::RecalcScroll()
  144. {
  145.     XGScrollView *v;
  146.     Rect h;
  147.     short page;
  148.     
  149.     v = dynamic_cast<XGScrollView *>(GetParent());
  150.     if (v == NULL) return;
  151.     
  152.     h = GetContentRect();
  153.     page = (h.bottom - h.top) / 12;
  154.     if (page < 1) page = 1;
  155.     
  156.     v->SetScrollPage(0,page);
  157.     page = fStart.Length() - page;
  158.     if (page < 0) page = 0;
  159.     v->SetScrollMax(0,page);
  160. }
  161.